﻿using eProcurement.BLL;
using eProcurement.DAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace eProcurement.members
{
    public partial class EditProject : System.Web.UI.Page
    {
        Manager manager = new Manager();

        protected void Page_Load( object sender, EventArgs e )
        {
            if ( !manager.IsPurchaser( Context.User.Identity.Name ) )
            {
                Response.Redirect( "../Signup2.aspx" );
            }

            if ( !IsPostBack )
            {
                SetupCategories();
                Project project = manager.GetMyProject( Convert.ToInt32( Request[ "pid" ] ) );
                if ( project != null )
                {
                    Hydrate( project );
                }
            }
        }

        private void Hydrate( Project project )
        {
            txtProjectTitle.Value = project.Title;
            txtCloseDate.Value = project.CloseDate.ToShortDateString();
            txtDescription.Value = project.Description;
            ddlIndustryTag.Text = project.IndustryCategory;
            ddlProjectState.Text = project.State;
        }

        private void SetupCategories()
        {
            ddlIndustryTag.DataSource = Manager.GetIndustryCategories( Server.MapPath( "../Xml/IndustryCategory.xml" ) );
            ddlIndustryTag.DataBind();
        }

        protected void btnSubmit_Click( object sender, EventArgs e )
        {
            Project project = new Project();

            project.Title = txtProjectTitle.Value;
            project.CloseDate = Convert.ToDateTime( txtCloseDate.Value );
            project.Description = txtDescription.Value;
            project.IndustryCategory = ddlIndustryTag.Text;

            project.ScopeDocument = fuScope.FileBytes;
            project.ScopeFileName = fuScope.FileName;
            project.AdditionalDocument = fuSupporting.FileBytes;
            project.AdditionalDocsName = fuSupporting.FileName;

            project.State = ddlProjectState.Text;
            project.Id = Convert.ToInt32( Request[ "pid" ] );

            if ( manager.UpdateProject( project ) )
            {
                tipSaved.Visible = true;
            }
        }

        protected void btnCancelProject_Click( object sender, EventArgs e )
        {
            manager.CancelProject( Convert.ToInt32( Request[ "pid" ] ) );
        }
    }
}